/**
 *
 */
package com.sb.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;

import java.io.*;

/**
 */
public class FileUtils {
    private final static Logger logger= LoggerFactory.getLogger(FileUtils.class);

    private final static String FILE_DELIMITER = "/";

    /**
     * 写入文件
     *
     * @param filePath
     * @param fileContent
     * @param charset
     * @throws IOException
     */
    public static void writer(String filePath, String fileContent,
                              String charset) throws IOException {
        FileOutputStream out = new FileOutputStream(filePath);
        InputStream in = IOUtils.getStringInputStream(fileContent);
        FileCopyUtils.copy(in, out);

    }

    /**
     * 读取文件
     *
     * @param filePath
     * @param charset
     * @return
     * @throws IOException
     */
    public static String reader(String filePath, String charset) throws IOException {
        FileInputStream in = new FileInputStream(filePath);
        String context = IOUtils.getInputStreamString(in);
        return context;
    }

    public static String save(String name, String text) {

        String path = "sb";
        File folder = new File(path);
        //判断uid文件夹是否存在,不存在就创建
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String path1 = folder + FILE_DELIMITER + name + ".html";

        //文件拷贝
        try {
            writer(path1, text, "utf-8");
        } catch (IOException e) {

            logger.info("保存文件失败", e.getMessage());
            return "保存失败";
        }
        logger.info("保存成功,path:" + path);
        return "保存成功";
    }

}
